<template>
  <div class="save-content">
    <div class="save-card">
      <a-card size="small" class="card" :bordered="false" :hoverable="true">
        <a-select ref="select" v-model:value="systemValue" style="width: 100%" @change="systemSelect">
          <a-select-option v-for="item in systemOptions" :key="item.value" :value="item.value">{{ item.label }}</a-select-option>
        </a-select>
        <div class="model-list">
          <div
            class="model-item"
            v-for="(item, index) in modelList"
            :key="index"
            :class="{ active: modelActive === item.value }"
            @click="selectModel(item)"
          >
            {{ item.label }}
          </div>
        </div>
      </a-card>
    </div>
    <div class="info-card">
      <div class="info-form">
        <a-card size="small" class="card" :bordered="false" :hoverable="true">
          <div class="list">
            <div class="item" v-for="item in formOption" :key="item.prop">
              <div class="label">{{ item.label }}:</div>
              <div class="span" v-if="item.format">{{ item.format(formData) }}</div>
              <div class="span" v-else>{{ formData[item.prop] || '-' }}</div>
            </div>
          </div>
        </a-card>
      </div>
      <div class="info-attr">
        <a-card size="small" class="card" :bordered="false" :hoverable="true">
          <a-table size="small" :dataSource="tableData" :columns="attrColumn" rowKey="attrId" bordered :loading="tableLoading" :pagination="false">
            <template #bodyCell="{ record, column }">
              <template v-if="column.dataIndex === 'time'">
                {{ dateFormat(record.time) }}
              </template>
            </template>
          </a-table>
          <div class="smart-query-table-page">
            <a-pagination
              showSizeChanger
              showQuickJumper
              show-less-items
              :pageSizeOptions="PAGE_SIZE_OPTIONS"
              :defaultPageSize="queryForm.pageSize"
              v-model:current="queryForm.pageNum"
              v-model:pageSize="queryForm.pageSize"
              :total="total"
              @change="queryData"
              @showSizeChange="queryData"
              :show-total="(total) => `共${total}条`"
            />
          </div>
        </a-card>
      </div>
    </div>
  </div>
</template>
<script setup>
  import { ref, onMounted, onUnmounted, watch, computed } from 'vue';
  import { getDictLabel, PageMixin } from '/@/utils/mixin';
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { iotApi } from '/@/api/plat/dataStoreRule';
  import { iotApi as iotAttrApi } from '/@/api/plat/dataModelAttr';
  import { dateFormat } from '/@/utils/date';
  import { systemOptions, modelOptions, columnOptions, tableOptions, formOptions } from './options';

  const { total, tableLoading, selectedRowKeyList, resetQueryForm, queryTableData, deleteFn, batchDeleteFn, queryFormState, watchTotal } =
    PageMixin();

  let defalutData = {
    name: undefined,
  };
  let queryForm = ref(Object.assign({}, queryFormState, defalutData));
  watchTotal(queryForm, queryData);

  const systemValue = ref('1');
  const modelList = ref(modelOptions[systemValue.value]);
  const modelActive = ref('1');
  const attrColumn = ref(columnOptions[modelActive.value] || []);
  const tableData = ref(tableOptions[modelActive.value] || []);
  total.value = tableData.value.length;
  const formData = ref(formOptions[modelActive.value] || {});
  const formOption = ref([
    {
      label: '指标名称',
      prop: 'code1',
    },
    {
      label: '接口类型',
      prop: 'code6',
    },
    {
      label: '指标方案',
      prop: 'code2',
    },
    {
      label: '关联模型',
      prop: 'code5',
    },
    {
      label: '接口地址',
      prop: 'code4',
    },
    {
      label: '指标描述',
      prop: 'code3',
    },
  ]);

  // 切换系统
  function systemSelect(val) {
    console.log('val: ', val);
  }
  function selectModel(row) {
    modelActive.value = row.value;
    attrColumn.value = columnOptions[modelActive.value] || [];
    tableData.value = tableOptions[modelActive.value] || [];
    total.value = tableData.value.length;
    formData.value = formOptions[modelActive.value] || {};
  }

  // 查询数据
  function queryData() {
    console.log('queryForm.value: ', queryForm.value);
    const params = {
      current: queryForm.value.pageNum,
      size: queryForm.value.pageSize,
      tableName: queryForm.value.tableName,
    };
    queryTableData(iotApi.queryPage1, params);
  }
</script>

<style scoped lang="less">
  .save-content {
    height: calc(100% - 20px);
    overflow: hidden;
    display: flex;
    justify-content: space-between;
    .save-card {
      width: 30%;
      height: 100%;
      .model-list {
        padding: 20px 0;
        .model-item {
          height: 30px;
          line-height: 30px;
          padding-left: 10px;
        }
        .model-item.active {
          color: #1677ff;
        }
      }
    }
    .info-card {
      width: calc(70% - 20px);
      height: 100%;
      .info-form {
        height: 200px;
        .list {
          height: 150px;
          display: flex;
          flex-wrap: wrap;
          flex-direction: column;
          .item {
            width: 50%;
            height: 50px;
            display: flex;
            align-items: center;
            .label {
              width: 120px;
              text-align: right;
              font-weight: 700;
              color: #000;
              margin-right: 10px;
            }
            .span {
              width: calc(100% - 150px);
              color: #000;
            }
          }
        }
      }
      .info-attr {
        height: calc(100% - 200px - 20px);
        margin-top: 20px;
      }
    }
  }
  .card {
    height: 100%;
  }
  ::v-deep .ant-card-body {
    height: 100%;
  }
</style>
